<?php

namespace app\store\model;

use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;

class ApplyOfflineShop extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'create_time';

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * 编辑店铺
     */
    public function editShop($fields, $uid, $store_id, $data, $offline_shop_id)
    {
        $data['logo'] = parse_url($data['logo'])['path'];
        $data['business_license'] = parse_url($data['business_license'])['path'];
        $bg_img = explode(',', $data['bg_imgs']);
        foreach ($bg_img as $k => $v) {
            $bg_img[$k] = parse_url($v)['path'];
        }
        $data['bg_imgs'] = implode(',', $bg_img);
        $values = $this->judceFileds($fields, $uid, $store_id, $data);
        if (!empty($values)) {
            $data['desc'] = implode(',', $values);
        } else {
            $data['desc'] = '修改店铺';
        }
        $data['offline_shop_id'] = $offline_shop_id;
        $data['uid'] = $uid;
        $data['type'] = 2;
        return $this->save($data);
    }

    /**
     * 判断要修改的内容
     */
    public function judceFileds($fields, $uid, $store_id, $data)
    {
        $rst = Db::name('offline_shop')->field(implode(',', $fields))->where(['uid' => $uid, 'delete_time' => 0, 'store_id' => $store_id])
            ->order('create_time', 'desc')->find();

        $field = ['category_id' => '分类', 'logo' => '店铺图片', 'label_id'=>'店铺标签','bg_imgs' => '店铺背景图片', 'lat' => '店铺位置', 'lng' => '店铺位置', 'legal_person' => '法人',
            'contact_phone' => '店铺联系方式', 'shop_name' => '店铺名称',
            'province' => '店铺位置', 'city' => '店铺位置', 'area' => '店铺位置', 'address' => '店铺位置', 'business_license' => '店铺营业执照'];
       
        $result = array_diff($data, $rst);
        if (!empty($result)) {
            $keys = array_intersect_key($field, $result);
            if (!empty($keys)) {
                $values = array_unique(array_values($keys));
                return $values;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
